package com.example.cardserver.service.impl;

import com.example.cardserver.entity.Goods;
import com.example.cardserver.mapper.GoodsMapper;
import com.example.cardserver.service.IGoodsService;
import com.example.cardserver.service.ex.GoodsException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    
    @Override
    public List<Goods> findList() {
        List<Goods> list = goodsMapper.findList();
        return list;
    }

    @Override
    public Goods findById(Integer id) {
        Goods goods = goodsMapper.findById(id);
        if(goods == null){
            throw new GoodsException("未查找到商品");
        }
        return goods;
    }

    @Override
    public Goods findByTitle(String title) {
        Goods goods = goodsMapper.findByTitle(title);
        if(goods ==null){
            throw new GoodsException("未查找到商品");
        }
        return goods;
    }

    @Override
    public void addNew(Goods goods, Integer price, String avatar) {
        String goodsTitle = goods.getTitle();
        Goods result = goodsMapper.findByTitle(goodsTitle);
        if(result!=null){
            throw new GoodsException("已有同名商品");
        }
        
    }

    @Override
    public void changePicture(Integer id, String picture) {
        Goods result = goodsMapper.findById(id);
        if(result == null){
            throw new GoodsException("商品不存在");
        }
        Integer row = goodsMapper.changePicture(id, picture);
        if(row !=1){
            throw new GoodsException("更新时出现异常");
        }
    }
}
